/*
 * ================================================================================变量定义区
 */


/*
 * ================================================================================页面初始化函数
 */
$(function () 
{
    //初始化统计结果对应的tab标题列表
    $("#tabs").tabs();
    
    //显示百分比宽度
    var bars = $("div[id^='bar_']");
    $.each(bars, function (n) 
    {
        $(this).show(function () 
        {
            var mywidth = Number($(this).attr("opwidth")) * 500 + "px";
            $(this).animate(
            {
                width: mywidth
            }
                , 500, null, function ()
            {
                var divId = $(this).attr("id") + "_data";
                $(divId).show();
            });
        });
    });

    //调查问卷信息
    $("#tab1").on("click", function () 
    {
    	var serveyId = $("#surveyId").val();
    	$("#tabs-1").load(BASE_PATH + "/survey/surveyStatistics/surveyResultStatistics/surveyTab1_sitemesh_frame.do?survey_id=" + serveyId );
    });

    //投票状态信息
    $("#tab2").on("click", function () 
    {
    	var serveyId = $("#surveyId").val();
    	$("#tabs-2").load(BASE_PATH + "/survey/surveyStatistics/surveyResultStatistics/surveyTab2_sitemesh_frame.do?survey_id=" + serveyId );
    });
    
    //引入数量统计信息
    $("#tab6").on("click", function () 
    {
    	var serveyId = $("#surveyId").val();
        $("#tabs-6").load(BASE_PATH + "/survey/surveyStatistics/qualitySurveyStatistics/surveyinfo_sitemesh_frame.do?survey_id=" + serveyId);
    });
    
    //跳转至已结束问卷列表
    $("#button6").click(function () 
    {
    	var type=$("#submitcurrentpage [name=type]").val();
    	if(type=="finished")
    	{
    		$("#submitcurrentpage").submit();
    	}else{
    		$("#submitcurrentpage1").submit();
    	}
    });
    
    //组合统计按钮绑定事件
    $("#button").click(groupingStaticClick);
    
    //组合统计下载事件绑定
    $('#download').click(function () 
    {
        $('#groupStaticsDownload').submit();
    });

});


/*
 * ================================================================================函数定义区
 */

/**
 * 处理选项对应的ABC的函数
 */
function dealWithString(str)
{
    var obj = {};
    var upperLitter = "ABCDEFGHI";
    var array = upperLitter.split("");
    for (var i = 0; i < array.length; i++)
    {
        obj[(i + 1) + "."] = array[i] + ".";
    }

    for (var key in obj)
    {
        str = str.replace(key, obj[key]);
    }

    return str;
}

/**
 * 组合统计按钮点击的事件
 */
function groupingStaticClick()
{
    var chks = $("input:checked");
    var mydata = "?";
    if (chks.length == 0)
    {
        openAlert("您没有选择任何题");
        return false;
    }

    var table = $("<table class='question-title'  border='1px' cellspacing='0px' align='center' style='margin-left:auto;margin-right:auto;'>");
    var v1_tr = $("<tr><td align='center' valign='top' colspan='3'>组合统计信息</td></tr>");
    v1_tr.appendTo(table);
    var v2_tr = $("<tr><td align='left' valign='top' colspan='3'>组合统计题目:</td></tr>");
    v2_tr.appendTo(table);
    $.each(chks, function (n)
    {
        var name = "checked_" + n + "=";
        var value = this.value;
        var elemet = "&" + name + value;
        mydata = mydata + elemet;
        var next = $(this).parent().next();
        var html = "";
        next.find('div').filter(":even").each(function ()
        {
            html += "<div>&nbsp;&nbsp;&nbsp;&nbsp;" + $(this).text() + "</div>";
        });
        var var_tr = $("<tr><td align='left' valign='top' colspan='3'>&nbsp;&nbsp;" + value + "." + $(this).attr("content") + html + "</td></tr>");
        var_tr.appendTo(table);
    });

    var v_tr = $("<tr><td align='left' valign='top' colspan='3'>组合统计结果:</td></tr>");
    v_tr.appendTo(table);
    var surveyId = $("#surveyId").attr("value");
    var url = BASE_PATH + "/survey/surveyStatistics/groupingStatistics/statisticSurvey/" + surveyId + ".do" + mydata;
    $.ajax(
        {
        type: "POST",  //提交方式
        url: url,//路径
        dataType: 'json',
        success: function (result)
        {
            //返回数据根据结果进行相应的处理
            $("#result").empty();
            $("#download").css('display', 'inline-block');
            $(result).each(function ()
            {
                var tr = $("<tr></tr>");
                tr.appendTo(table);
                var td = $("<td align='left' valign='top'>&nbsp;&nbsp;" + "组合项:" + this.key + "</td>" + "<td align='left' valign='top'>" + "[" + this.value + "票]" + "</td>");
                td.appendTo(tr);
            });
            table.appendTo($("#result"));
            $("#result").append("</table>");
        }
    });
    $("#tab3").css('display', 'block');
    $("#tabs-3").css('display', 'block');

    var checkedBoxValue = "";
    $("#tabs-1 input[type='checkbox']").each(function ()
    {
        if ($(this).attr("checked") == 'checked')
        {
            checkedBoxValue += $(this).val() + ',';
        }
    });

    if (checkedBoxValue.length > 0)
    {
        $('#tabs-3 input[name="selectedQuestionOrder"]').val(checkedBoxValue.substr(0, checkedBoxValue.length - 1));
    }

    $("#tabs").tabs({active: 2});
}
